﻿Imports System.IO
Imports System.Xml

Public Class Writer   
    Private Shared SitemapFileName = "\Web.sitemap"
    Private Shared originalSitemapResxFileName = "\Web.sitemap.resx"
    Private Shared SitemapResxFileName = ""


    Public Shared Sub writeSitemap(ByVal doc As XmlDocument)
        initializeFile(SitemapFileName)
        Dim sets As New XmlWriterSettings
        sets.Indent = True
        Dim wr As XmlWriter = XmlWriter.Create(Storage.LocalizationPath & SitemapFileName, sets)
        doc.WriteContentTo(wr)
        wr.Close()
    End Sub

    Private Shared Sub initializeFile(ByVal fileName As String)
        File.Create(Storage.LocalizationPath & fileName).Close()
    End Sub

    Public Shared Sub writeLocalization(ByVal value As String)
        File.AppendAllText(Storage.LocalizationPath & SitemapResxFileName, value & vbCrLf)
    End Sub

    Public Shared Sub writeLocalizationHeader(ByVal language As String)
        If String.IsNullOrEmpty(language) Then
            SitemapResxFileName = originalSitemapResxFileName
            'initializeFile(originalSitemapResxFileName)
        Else
            SitemapResxFileName = originalSitemapResxFileName.Replace(".resx", "." & language & ".resx")
            'initializeFile(SitemapResxFileName)
        End If
        initializeFile(SitemapResxFileName)

        writeLocalization("<?xml version=""1.0"" encoding=""utf-8""?>")
        writeLocalization("<root>")
        writeLocalization("  <xsd:schema id=""root"" xmlns="""" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">")
        writeLocalization("    <xsd:import namespace=""http://www.w3.org/XML/1998/namespace"" />")
        writeLocalization("    <xsd:element name=""root"" msdata:IsDataSet=""true"">")
        writeLocalization("      <xsd:complexType>")
        writeLocalization("        <xsd:choice maxOccurs=""unbounded"">")
        writeLocalization("          <xsd:element name=""metadata"">")
        writeLocalization("            <xsd:complexType>")
        writeLocalization("              <xsd:sequence>")
        writeLocalization("                <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" />")
        writeLocalization("              </xsd:sequence>")
        writeLocalization("              <xsd:attribute name=""name"" use=""required"" type=""xsd:string"" />")
        writeLocalization("              <xsd:attribute name=""type"" type=""xsd:string"" />")
        writeLocalization("              <xsd:attribute name=""mimetype"" type=""xsd:string"" />")
        writeLocalization("              <xsd:attribute ref=""xml:space"" />")
        writeLocalization("            </xsd:complexType>")
        writeLocalization("          </xsd:element>")
        writeLocalization("          <xsd:element name=""assembly"">")
        writeLocalization("            <xsd:complexType>")
        writeLocalization("              <xsd:attribute name=""alias"" type=""xsd:string"" />")
        writeLocalization("              <xsd:attribute name=""name"" type=""xsd:string"" />")
        writeLocalization("            </xsd:complexType>")
        writeLocalization("          </xsd:element>")
        writeLocalization("          <xsd:element name=""data"">")
        writeLocalization("            <xsd:complexType>")
        writeLocalization("              <xsd:sequence>")
        writeLocalization("                <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" />")
        writeLocalization("                <xsd:element name=""comment"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""2"" />")
        writeLocalization("              </xsd:sequence>")
        writeLocalization("              <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" msdata:Ordinal=""1"" />")
        writeLocalization("              <xsd:attribute name=""type"" type=""xsd:string"" msdata:Ordinal=""3"" />")
        writeLocalization("              <xsd:attribute name=""mimetype"" type=""xsd:string"" msdata:Ordinal=""4"" />")
        writeLocalization("              <xsd:attribute ref=""xml:space"" />")
        writeLocalization("            </xsd:complexType>")
        writeLocalization("          </xsd:element>")
        writeLocalization("          <xsd:element name=""resheader"">")
        writeLocalization("            <xsd:complexType>")
        writeLocalization("              <xsd:sequence>")
        writeLocalization("                <xsd:element name=""value"" type=""xsd:string"" minOccurs=""0"" msdata:Ordinal=""1"" />")
        writeLocalization("              </xsd:sequence>")
        writeLocalization("              <xsd:attribute name=""name"" type=""xsd:string"" use=""required"" />")
        writeLocalization("            </xsd:complexType>")
        writeLocalization("          </xsd:element>")
        writeLocalization("        </xsd:choice>")
        writeLocalization("      </xsd:complexType>")
        writeLocalization("    </xsd:element>")
        writeLocalization("  </xsd:schema>")
        writeLocalization("  <resheader name=""resmimetype"">")
        writeLocalization("    <value>text/microsoft-resx</value>")
        writeLocalization("  </resheader>")
        writeLocalization("  <resheader name=""version"">")
        writeLocalization("    <value>2.0</value>")
        writeLocalization("  </resheader>")
        writeLocalization("  <resheader name=""reader"">")
        writeLocalization("    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>")
        writeLocalization("  </resheader>")
        writeLocalization("  <resheader name=""writer"">")
        writeLocalization("    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>")
        writeLocalization("  </resheader>")
    End Sub

    Public Shared Sub writeLocalizationFooter()
        writeLocalization("</root>")
    End Sub
End Class
